package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.ResearchInstitution;
import com.innovations.inn.domain.ResearchOutcomes;
import com.innovations.inn.domain.request.RequestFilterListPreview;
import com.innovations.inn.domain.request.RequestListPreview;
import com.innovations.inn.domain.request.RequestResearchImport;
import com.innovations.inn.domain.request.RequestResearchOutcomes;
import com.innovations.inn.domain.response.ResponseItemCount;
import com.innovations.inn.domain.response.ResponseListPreview;
import com.innovations.inn.domain.response.ResponseResearchOutcomesList;
import com.innovations.inn.service.IResearchInstitutionService;
import com.innovations.inn.service.IResearchOutcomesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 科研成果Controller
 *
 * @author dp
 * @date 2024-08-10
 */
@Api(tags = "科研成果")
@RestController
@RequestMapping("/inn/outcomes")
public class ResearchOutcomesController extends BaseController {
    @Autowired
    TokenService tokenService;
    @Autowired
    private IResearchOutcomesService researchOutcomesService;
    @Autowired
    IResearchInstitutionService researchInstitutionService;

    /**
     * 查询科研成果列表
     */
    @ApiOperation("查询科研成果列表")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResearchOutcomes researchOutcomes) {
        startPage();
        List<ResponseResearchOutcomesList> list = researchOutcomesService.selectResearchOutcomesList(researchOutcomes);
        return getDataTable(list);
    }

    /**
     * 查询用户待审核列表
     */
    @ApiOperation("查询用户待审核列表")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:list')")
    @GetMapping("/user/list")
    public TableDataInfo userList(Long userId, Long status) {
        ResearchInstitution researchOutcomes = new ResearchInstitution();
        researchOutcomes.setUserId(userId);
        List<ResearchInstitution> list = researchInstitutionService.selectResearchInstitutionList(researchOutcomes);
        if (list == null || list.isEmpty()) {
            throw new RuntimeException("当前用户无可管理机构");
        }
        ResearchOutcomes roInput = new ResearchOutcomes();

        roInput.setInstitutionId(list.get(0).getInstitutionId());

        if (status != null) {
            roInput.setState(status);
        }

        startPage();
        List<ResponseResearchOutcomesList> Returnlist = researchOutcomesService.selectResearchOutcomesList(roInput);
        return getDataTable(Returnlist);
    }

    /**
     * 查询科研成果列表合计
     */
    @ApiOperation("查询科研成果列表合计")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:list')")
    @GetMapping("/list/count")
    public TableDataInfo listCount(ResearchOutcomes researchOutcomes) {
        startPage();
        List<ResponseItemCount> listCount = researchOutcomesService.selectResearchOutcomesCount(researchOutcomes);
        return getDataTable(listCount);
    }

    /**
     * 查询科研成果列表-预览展示
     */
    @ApiOperation("查询科研成果列表-预览展示")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:list')")
    @PostMapping("/list/preview")
    public TableDataInfo preview(HttpServletRequest request, @RequestBody RequestListPreview researchOutcomes) {
        if (researchOutcomes.getUserId() == null) {
            researchOutcomes.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        }
        startPage();
        List<ResponseListPreview> list = researchOutcomesService.selectOutcomesPreview(researchOutcomes);
        return getDataTable(list);
    }


    /**
     * 查询科研成果列表-预览展示-过滤
     */
    @ApiOperation("查询科研成果列表-预览展示-过滤")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:list')")
    @PostMapping("/filter/list/preview")
    public TableDataInfo filterPreview(HttpServletRequest request, @RequestBody RequestFilterListPreview researchOutcomes) {
        if (researchOutcomes.getUserId() == null) {
            researchOutcomes.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        }
        startPage();
        List<ResponseListPreview> list = researchOutcomesService.selectOutcomesFilterPreview(researchOutcomes);
        return getDataTable(list);
    }

    /**
     * 获取科研成果详细信息
     */
    @ApiOperation("获取科研成果详细信息")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:query')")
    @GetMapping(value = "/{researchOutcomesId}")
    public AjaxResult getInfo(@PathVariable("researchOutcomesId") Long researchOutcomesId) {
        return success(researchOutcomesService.selectResearchOutcomesByResearchOutcomesId(researchOutcomesId));
    }

    /**
     * 新增科研成果
     */
    @ApiOperation("新增科研成果")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:add')")
    @Log(title = "科研成果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody RequestResearchOutcomes researchOutcomes) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        researchOutcomesService.completeData(researchOutcomes, userId, 1);
        return toAjax(researchOutcomesService.insertResearchOutcomes(researchOutcomes));
    }

    /**
     * 修改科研成果
     */
    @ApiOperation("修改科研成果")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:edit')")
    @Log(title = "科研成果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody RequestResearchOutcomes researchOutcomes) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        researchOutcomesService.completeData(researchOutcomes, userId, 2);
        return toAjax(researchOutcomesService.updateResearchOutcomes(researchOutcomes));
    }

    /**
     * 删除科研成果
     */
    @ApiOperation("删除科研成果")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:remove')")
    @Log(title = "科研成果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{researchOutcomesIds}")
    public AjaxResult remove(@PathVariable Long[] researchOutcomesIds) {
        return toAjax(researchOutcomesService.deleteResearchOutcomesByResearchOutcomesIds(researchOutcomesIds));
    }

    /**
     * 导入科研成果
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已经存在的用户数据
     * @return 结果
     */
    @ApiOperation("导入科研成果")
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<RequestResearchImport> util = new ExcelUtil<RequestResearchImport>(RequestResearchImport.class);
        List<RequestResearchImport> userList = util.importExcel(file.getInputStream(), 2);
        Long userId = getUserId();
        //Long userId = 164L;
        String message = researchOutcomesService.importResearch(userList, updateSupport, userId);
        return success(message);
    }

    /**
     * 导出科研成果
     */
    @ApiOperation("导出科研成果")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:export')")
    @Log(title = "科研成果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchOutcomes researchOutcomes) {
        List<RequestResearchImport> list = researchOutcomesService.selectExport(researchOutcomes);
        ExcelUtil<RequestResearchImport> util = new ExcelUtil<>(RequestResearchImport.class);
        util.exportExcel(response, list, "科研成果数据");
    }

}
